﻿Imports System.Data
Imports AutoDevModel
Imports Cls_Common
Imports SecurityService
Partial Class Control_TiepNhanMCC_List
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Sub and Function"
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
            If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
                ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "jqueryEnable", "ajaxJquery()", True)
            Else
                Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "jqueryEnable", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
            End If
            BindToGrid()
            btnDelete.Attributes.Add("onclick", "return confirmMultiDelete('" & btnDelete.ClientID & "');")
        End If
    End Sub
    Private Sub BindToGrid(Optional ByVal iPage As Integer = 1 _
          , Optional ByVal intHuyenId As Integer = -1 _
          , Optional ByVal strSoBienNhan As String = "" _
          , Optional ByVal strNgayTiepNhan As String = "" _
          , Optional ByVal strNgayHenTra As String = "" _
          , Optional ByVal strNoiDungHoSo As String = "" _
          , Optional ByVal strTenHoKinhDoanh As String = "" _
          , Optional ByVal strDiaChiKinhDoanh As String = "" _
          , Optional ByVal strNganhNghe As String = "" _
          , Optional ByVal strDienThoaiDD As String = "" _
          , Optional ByVal strDienThoaiBan As String = "" _
          , Optional ByVal strCMND As String = "" _
          , Optional ByVal strNguoiDaiDien As String = "" _
          , Optional ByVal strDiaChiNguoiDaiDien As String = "" _
          , Optional ByVal _ddlStatusId As Integer = 1 _
          , Optional ByVal _ddlTypeId As String = "" _
      )
        Using data As New AutoDevEntities
            Dim arrSearch() As String = {iPage.ToString, intHuyenId.ToString, strSoBienNhan.ToString, strNgayTiepNhan.ToString, strNgayHenTra.ToString, strNoiDungHoSo.ToString, strTenHoKinhDoanh.ToString, strDiaChiKinhDoanh.ToString, strNganhNghe.ToString, strDienThoaiDD.ToString, strDienThoaiBan.ToString, strCMND.ToString, strNguoiDaiDien.ToString, strDiaChiNguoiDaiDien.ToString, _ddlStatusId, _ddlTypeId}
            ViewState("search") = arrSearch
            'So ban ghi muon the hien tren trang
            intHuyenId = Session("HuyenId")
            Dim intPag_Size As Int32 = drpPage_Size.SelectedValue
            _ddlTypeId = ddlTypeId.SelectedValue
            _ddlStatusId = ddlStatusId.SelectedValue
            Dim p As List(Of uspHoSoSelectAll_Result) = data.uspHoSoSelectAll(intHuyenId _
                , strSoBienNhan _
                , strNgayTiepNhan _
                , strNgayHenTra _
                , strNoiDungHoSo _
                , strTenHoKinhDoanh _
                , strDiaChiKinhDoanh _
                , strNganhNghe _
                , strDienThoaiDD _
                , strDienThoaiBan _
                , strCMND _
                , strNguoiDaiDien _
                , strDiaChiNguoiDaiDien _
                , _ddlStatusId _
                , _ddlTypeId _
            , iPage, intPag_Size).ToList
            Dim strKey_Name() As String = {"Id", "HuyenId", "TypeId", "SoBienNhan"}
            'Tong so ban ghi
            If p.Count > 0 Then
                hidCount.Value = p.FirstOrDefault.Total()
                Create_Pager(hidCount.Value, iPage, intPag_Size, 10)
            Else
                hidCount.Value = 0
                With rptPage
                    .DataSource = Nothing
                    .DataBind()
                End With
            End If
            With grdShow
                .DataKeyNames = strKey_Name
                .DataSource = p
                .DataBind()
            End With
            If (hidCount.Value > 0) Then
                lblTotal.Text = "Hiển thị " + (((iPage - 1) * intPag_Size) + 1).ToString + " đến " + (((iPage - 1) * intPag_Size) + grdShow.Rows.Count).ToString + " trong tổng số " + hidCount.Value.ToString + " bản ghi."
            Else
                lblTotal.Text = ""
            End If
        End Using
    End Sub
    Sub Create_Pager(ByVal Total_Record As Integer, ByVal Page_Index As Integer, ByVal Page_Size As Integer, ByVal Page2Show As Integer)
        Dim TotalPage As Integer = IIf((Total_Record Mod Page_Size) = 0, Total_Record / Page_Size, Total_Record \ Page_Size + 1)
        'lu lai tong so ban ghi
        hidIndex_page.Value = TotalPage
        'gan lai curPage de set active
        hidCur_Page.Value = Page_Index
        'generate ra left page
        Dim cPageGenerate_left As IEnumerable(Of Integer)
        If Page_Index <= Page2Show Then
            cPageGenerate_left = Enumerable.Range(1, Page_Index)
        Else
            cPageGenerate_left = Enumerable.Range(Page_Index - Page2Show, Page2Show)
        End If
        'generate ra right page
        Dim cPageGenerate_Right As IEnumerable(Of Integer)
        If Page_Index + Page2Show <= TotalPage Then
            cPageGenerate_Right = Enumerable.Range(Page_Index, Page2Show + 1)
        Else
            cPageGenerate_Right = Enumerable.Range(Page_Index, TotalPage - Page_Index + 1)
        End If
        'union 2 range va bind to Grid
        With rptPage
            .DataSource = cPageGenerate_left.Union(cPageGenerate_Right)
            .DataBind()
        End With
    End Sub
    Protected Sub rptPage_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptPage.ItemDataBound
        Dim lnkTitle As LinkButton
        lnkTitle = e.Item.FindControl("lnkTitle")
        Dim ScriptManager As System.Web.UI.ScriptManager = System.Web.UI.ScriptManager.GetCurrent(Me.Page)
        ScriptManager.RegisterAsyncPostBackControl(lnkTitle)
        If e.Item.DataItem = hidCur_Page.Value Then
            lnkTitle.Text = "<span class='current'>" & e.Item.DataItem & "</span>"
        Else
            lnkTitle.Text = "<span>" & e.Item.DataItem & "</span>"
        End If
        lnkTitle.ToolTip = e.Item.DataItem
    End Sub
    Protected Sub lnkTitle_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim lnkTile As LinkButton = CType(sender, LinkButton)
        Dim arrSearch() As String
        arrSearch = ViewState("search")
        BindToGrid(CInt(lnkTile.ToolTip) _
       , arrSearch(1) _
       , arrSearch(2) _
       , arrSearch(3) _
       , arrSearch(4) _
       , arrSearch(5) _
       , arrSearch(6) _
       , arrSearch(7) _
       , arrSearch(8) _
       , arrSearch(9) _
       , arrSearch(10) _
       , arrSearch(11) _
       , arrSearch(12) _
       , arrSearch(13) _
       , arrSearch(14) _
       , arrSearch(15) _
        )
        lnkLast.Enabled = True
        lnkFirst.Enabled = True
        If CInt(lnkTile.ToolTip) = hidIndex_page.Value Then
            lnkLast.Enabled = False
        ElseIf CInt(lnkTile.ToolTip) = 1 Then
            lnkFirst.Enabled = False
        End If
    End Sub
    Protected Sub lnkFirst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFirst.Click
        If hidCur_Page.Value > 1 Then
            hidCur_Page.Value = hidCur_Page.Value - 1
        End If
        BindToGrid(hidCur_Page.Value)
    End Sub
    Protected Sub lnkLast_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkLast.Click
        hidCur_Page.Value = hidCur_Page.Value + 1
        BindToGrid(hidCur_Page.Value)
    End Sub
#End Region
#Region "Event for control"
    Protected Sub lnkbtnDelete_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim intId As Integer
        Dim strLogName As String = ""
        Using data As New AutoDevEntities
            intId = grdShow.DataKeys(hidID.Value)("Id").ToString
            Dim q = (From p In data.HoSoes Where p.Id = intId Select p).FirstOrDefault
            Try
                data.HoSoes.DeleteObject(q)
                data.SaveChanges()
                'Insert_App_Log("Insert  HoSo:" & txtTitle.Text.Trim & "", Function_Name.HoSo, Audit_Type.Create, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                Excute_Javascript("Alertbox('Xóa dữ liệu thành công.');", Me.Page, True)
            Catch ex As Exception
                log4net.Config.XmlConfigurator.Configure()
                log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                Excute_Javascript("Alertbox('Xóa thất bại.');", Me.Page, True)
            End Try
        End Using
        BindToGrid()
    End Sub
    Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        Dim intId As Integer = 0
        Dim intCount As Integer
        Dim intTotal As Integer
        Using data As New AutoDevEntities
            Try
                For Each item As GridViewRow In grdShow.Rows
                    Dim chkItem As New CheckBox
                    chkItem = CType(item.FindControl("chkItem"), CheckBox)
                    If chkItem.Checked Then
                        intTotal += 1
                        intId = grdShow.DataKeys(item.RowIndex)("Id").ToString
                        Dim q = (From p In data.HoSoes Where p.Id = intId Select p).FirstOrDefault
                        Try
                            data.HoSoes.DeleteObject(q)
                            'Insert_App_Log("Insert  HoSo:" & txtTitle.Text.Trim & "", Function_Name.HoSo, Audit_Type.Create, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                            data.SaveChanges()
                            intCount += 1
                        Catch ex As Exception
                        End Try
                    End If
                Next
                If intCount > 0 Then
                    Excute_Javascript("Alertbox('Xóa dữ liệu thành công. " & intCount.ToString & " /" & intTotal.ToString & " record.');", Me.Page, True)
                Else
                    Excute_Javascript("Alertbox('Xóa thất bại.');", Me.Page, True)
                End If
            Catch ex As Exception
                log4net.Config.XmlConfigurator.Configure()
                log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                Excute_Javascript("Alertbox('Xóa thất bại.');", Me.Page, True)
            End Try
        End Using
        BindToGrid()
    End Sub
    Protected Sub grdShow_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdShow.RowDataBound
        If e.Row.RowIndex >= 0 Then
            Dim lblSTT As Label = CType(e.Row.FindControl("lblSTT"), Label)
            lblSTT.Text = CInt(drpPage_Size.SelectedValue) * (CInt(hidCur_Page.Value) - 1).ToString + e.Row.RowIndex + 1
            Dim lnkbtnDelete As LinkButton = CType(e.Row.FindControl("lnkbtnDelete"), LinkButton)
            lnkbtnDelete.Attributes.Add("onclick", "return ComfirmDialog('" + drpMessage.Items(0).Text + "', 0,'" + lnkbtnDelete.ClientID + "','" + e.Row.RowIndex.ToString + "',1);")
            Dim hplTitle As HyperLink = CType(e.Row.FindControl("hplTitle"), HyperLink)
            hplTitle.NavigateUrl = "../../Page/TiepNhan/UpdateMcc.aspx?s=" & grdShow.DataKeys(e.Row.RowIndex)("SoBienNhan").ToString
            hplTitle.Text = grdShow.DataKeys(e.Row.RowIndex)("SoBienNhan").ToString

            Dim hplEdit As HyperLink = CType(e.Row.FindControl("hplEdit"), HyperLink)
            Dim ltrLoaiHoSo As Literal = CType(e.Row.FindControl("ltrLoaiHoSo"), Literal)
            If grdShow.DataKeys(e.Row.RowIndex)("TypeId").ToString = 1 Then
                ltrLoaiHoSo.Text = "chấm dứt"
            ElseIf grdShow.DataKeys(e.Row.RowIndex)("TypeId").ToString = 3 Then
                ltrLoaiHoSo.Text = "cấp đổi"
            ElseIf grdShow.DataKeys(e.Row.RowIndex)("TypeId").ToString = 2 Then
                ltrLoaiHoSo.Text = "cấp mới"
            ElseIf grdShow.DataKeys(e.Row.RowIndex)("TypeId").ToString = 4 Then
                ltrLoaiHoSo.Text = "cấp lại"
            End If
            hplEdit.NavigateUrl = "../../Page/QuanLyHoSo/XuLyLoaiMotCuaImport.aspx?Id=" & grdShow.DataKeys(e.Row.RowIndex)("Id").ToString
            Dim hplHuyenid As HyperLink = CType(e.Row.FindControl("hplHuyenid"), HyperLink)
        End If
    End Sub
#End Region
#Region "Search"
    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        BindToGrid(1 _
             , Session("HuyenId") _
            , txtSobiennhan.Text.Trim _
            , txtNgaytiepnhan.Text.Trim _
            , txtNgayhentra.Text.Trim _
            , txtNoidunghoso.Text.Trim _
            , txtTenhokinhdoanh.Text.Trim _
            , txtDiachikinhdoanh.Text.Trim _
            , txtNganhnghe.Text.Trim _
            , txtDienthoaidd.Text.Trim _
            , txtDienthoaiban.Text.Trim _
            , txtCmnd.Text.Trim _
            , txtNguoidaidien.Text.Trim _
            , txtDiachinguoidaidien.Text.Trim _
            , ddlStatusId.SelectedValue _
            , ddlTypeId.SelectedValue _
            )
    End Sub
    Protected Sub drpPage_Size_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles drpPage_Size.SelectedIndexChanged
        Dim arrSearch() As String
        arrSearch = ViewState("search")
        BindToGrid(1, CInt(arrSearch(1).ToString) _
        , arrSearch(2).ToString _
        , arrSearch(3).ToString _
        , arrSearch(4).ToString _
        , arrSearch(5).ToString _
        , arrSearch(6).ToString _
        , arrSearch(7).ToString _
        , arrSearch(8).ToString _
        , arrSearch(9).ToString _
        , arrSearch(10).ToString _
        , arrSearch(11).ToString _
        , arrSearch(12).ToString _
        , arrSearch(13).ToString _
        , arrSearch(14).ToString _
        , arrSearch(15).ToString _
         )
    End Sub
    Protected Sub btnReset_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReset.Click

        ' txtHuyenid.Text = ""
        txtSobiennhan.Text = ""
        txtNgaytiepnhan.Text = ""
        txtNgayhentra.Text = ""
        txtNoidunghoso.Text = ""
        txtTenhokinhdoanh.Text = ""
        txtDiachikinhdoanh.Text = ""
        txtNganhnghe.Text = ""
        txtDienthoaidd.Text = ""
        txtDienthoaiban.Text = ""
        txtCmnd.Text = ""
        txtNguoidaidien.Text = ""
        txtDiachinguoidaidien.Text = ""
        ddlStatusId.SelectedIndex = -1
        ddlTypeId.SelectedIndex = -1
        drpPage_Size.SelectedIndex = -1
        BindToGrid(1)

    End Sub
#End Region
End Class
